/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GroupChat;

import Client.Client;
import Client.ClientUtils;
import java.awt.Color;
import javax.swing.DefaultListModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

/**
 *
 * @author TienNT
 */
public class GroupConversasion extends javax.swing.JFrame {

    Client mClient;
    int mGroupId;
    StyledDocument doc;
    DefaultListModel<String> listContactModel = new DefaultListModel<>();

    /**
     * Creates new form GroupConversasion
     */
    public GroupConversasion(Client client, int groupId) {
        mClient = client;
        mGroupId = groupId;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listJoinedContact = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        textPaneConversasion = new javax.swing.JTextPane();
        txtMessage = new javax.swing.JTextField();
        btnSend = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        listJoinedContact.setModel(listContactModel);
        listJoinedContact.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(listJoinedContact);

        doc = textPaneConversasion.getStyledDocument();
        addStylesToDocument(doc);
        textPaneConversasion.setEditable(false);
        jScrollPane2.setViewportView(textPaneConversasion);

        txtMessage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMessageActionPerformed(evt);
            }
        });

        btnSend.setText("Send");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSend)
                        .addGap(0, 2, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSend, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addStylesToDocument(StyledDocument doc) {
        //Initialize some styles.
        Style def = StyleContext.getDefaultStyleContext().
                getStyle(StyleContext.DEFAULT_STYLE);

        Style fromOther = doc.addStyle("fromOther", def);
        StyleConstants.setBold(fromOther, true);
        StyleConstants.setForeground(fromOther, new Color(51, 51, 153));

        Style fromMe = doc.addStyle("fromMe", def);
        StyleConstants.setBold(fromMe, true);
        StyleConstants.setForeground(fromMe, new Color(128, 128, 128));

        Style content = doc.addStyle("content", def);
        StyleConstants.setForeground(content, Color.black);

        Style notify = doc.addStyle("notify", def);
        StyleConstants.setBold(notify, true);
        StyleConstants.setForeground(notify, new Color(102, 102, 102));
        StyleConstants.setBackground(notify, new Color(247, 247, 247));
    }

    public void updateConversasion(String from, String message) {
        try {
            if (from.equals(mClient.getClientName())) {
                doc.insertString(doc.getLength(), from + "  :  ",
                        doc.getStyle("fromMe"));
                doc.insertString(doc.getLength(), message + "\n",
                        doc.getStyle("content"));
            } else if (from.equals("notify")) {
                doc.insertString(doc.getLength(), message + "\n",
                        doc.getStyle("notify"));
            } else {
                doc.insertString(doc.getLength(), from + "  :  ",
                        doc.getStyle("fromOther"));
                doc.insertString(doc.getLength(), message + "\n",
                        doc.getStyle("content"));
            }

        } catch (BadLocationException ex) {
        }
    }

    private void txtMessageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMessageActionPerformed
        // TODO add your handling code here:
        if (txtMessage.getText().equals("")) {
            return;
        }
        mClient.sendMessageToServer(ClientUtils.NEW_GROUP_MESSAGE + ":" + mGroupId + ":" + txtMessage.getText());
        txtMessage.setText("");
    }//GEN-LAST:event_txtMessageActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        mClient.sendMessageToServer(ClientUtils.REQUEST_OUT_GROUP + ":" + mGroupId);
    }//GEN-LAST:event_formWindowClosing

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        // TODO add your handling code here:
        if (txtMessage.getText().equals("")) {
            return;
        }
        mClient.sendMessageToServer(ClientUtils.NEW_GROUP_MESSAGE + ":" + mGroupId + ":" + txtMessage.getText());
        txtMessage.setText("");
    }//GEN-LAST:event_btnSendActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSend;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listJoinedContact;
    private javax.swing.JTextPane textPaneConversasion;
    private javax.swing.JTextField txtMessage;
    // End of variables declaration//GEN-END:variables
}
